<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-comment">-- | See GHC #10762 and #15021.</span><span>
</span><span id="line-2"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.HandleEncoding</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.HandleEncoding.html#configureHandleEncoding"><span class="hs-identifier">configureHandleEncoding</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-3"></span><span>
</span><span id="line-4"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Prelude.html#"><span class="hs-identifier">Prelude</span></a></span><span> </span><span class="hs-comment">-- See note [Why do we import Prelude here?]</span><span>
</span><span id="line-5"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.Encoding.html#"><span class="hs-identifier">GHC.IO.Encoding</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/GHC.IO.Encoding.Types.html#textEncodingName"><span class="hs-identifier">textEncodingName</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-6"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/System.Environment.html#"><span class="hs-identifier">System.Environment</span></a></span><span>
</span><span id="line-7"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/System.IO.html#"><span class="hs-identifier">System.IO</span></a></span><span>
</span><span id="line-8"></span><span>
</span><span id="line-9"></span><span class="hs-comment">-- | Handle GHC-specific character encoding flags, allowing us to control how</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- GHC produces output regardless of OS.</span><span>
</span><span id="line-11"></span><span class="annot"><a href="GHC.HandleEncoding.html#configureHandleEncoding"><span class="hs-identifier hs-type">configureHandleEncoding</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-12"></span><span id="configureHandleEncoding"><span class="annot"><span class="annottext">configureHandleEncoding :: IO ()
</span><a href="GHC.HandleEncoding.html#configureHandleEncoding"><span class="hs-identifier hs-var hs-var">configureHandleEncoding</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-13"></span><span>   </span><span id="local-6989586621679057470"><span class="annot"><span class="annottext">[(String, String)]
</span><a href="#local-6989586621679057470"><span class="hs-identifier hs-var">env</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IO [(String, String)]
</span><a href="../../base/src/System.Environment.html#getEnvironment"><span class="hs-identifier hs-var">getEnvironment</span></a></span><span>
</span><span id="line-14"></span><span>   </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">String -&gt; [(String, String)] -&gt; Maybe String
forall a b. Eq a =&gt; a -&gt; [(a, b)] -&gt; Maybe b
</span><a href="../../base/src/GHC.List.html#lookup"><span class="hs-identifier hs-var">lookup</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC_CHARENC&quot;</span></span><span> </span><span class="annot"><span class="annottext">[(String, String)]
</span><a href="#local-6989586621679057470"><span class="hs-identifier hs-var">env</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-15"></span><span>    </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;UTF-8&quot;</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-16"></span><span>     </span><span class="annot"><span class="annottext">Handle -&gt; TextEncoding -&gt; IO ()
</span><a href="../../base/src/GHC.IO.Handle.html#hSetEncoding"><span class="hs-identifier hs-var">hSetEncoding</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="../../base/src/GHC.IO.StdHandles.html#stdout"><span class="hs-identifier hs-var">stdout</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="../../base/src/GHC.IO.Encoding.html#utf8"><span class="hs-identifier hs-var">utf8</span></a></span><span>
</span><span id="line-17"></span><span>     </span><span class="annot"><span class="annottext">Handle -&gt; TextEncoding -&gt; IO ()
</span><a href="../../base/src/GHC.IO.Handle.html#hSetEncoding"><span class="hs-identifier hs-var">hSetEncoding</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="../../base/src/GHC.IO.StdHandles.html#stderr"><span class="hs-identifier hs-var">stderr</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="../../base/src/GHC.IO.Encoding.html#utf8"><span class="hs-identifier hs-var">utf8</span></a></span><span>
</span><span id="line-18"></span><span>    </span><span class="annot"><span class="annottext">Maybe String
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-19"></span><span>     </span><span class="hs-comment">-- Avoid GHC erroring out when trying to display unhandled characters</span><span>
</span><span id="line-20"></span><span>     </span><span class="annot"><span class="annottext">Handle -&gt; IO ()
</span><a href="GHC.HandleEncoding.html#hSetTranslit"><span class="hs-identifier hs-var">hSetTranslit</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="../../base/src/GHC.IO.StdHandles.html#stdout"><span class="hs-identifier hs-var">stdout</span></a></span><span>
</span><span id="line-21"></span><span>     </span><span class="annot"><span class="annottext">Handle -&gt; IO ()
</span><a href="GHC.HandleEncoding.html#hSetTranslit"><span class="hs-identifier hs-var">hSetTranslit</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="../../base/src/GHC.IO.StdHandles.html#stderr"><span class="hs-identifier hs-var">stderr</span></a></span><span>
</span><span id="line-22"></span><span>
</span><span id="line-23"></span><span class="hs-comment">-- | Change the character encoding of the given Handle to transliterate</span><span>
</span><span id="line-24"></span><span class="hs-comment">-- on unsupported characters instead of throwing an exception</span><span>
</span><span id="line-25"></span><span class="annot"><a href="GHC.HandleEncoding.html#hSetTranslit"><span class="hs-identifier hs-type">hSetTranslit</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.Handle.Types.html#Handle"><span class="hs-identifier hs-type">Handle</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-26"></span><span id="hSetTranslit"><span class="annot"><span class="annottext">hSetTranslit :: Handle -&gt; IO ()
</span><a href="GHC.HandleEncoding.html#hSetTranslit"><span class="hs-identifier hs-var hs-var">hSetTranslit</span></a></span></span><span> </span><span id="local-6989586621679057462"><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679057462"><span class="hs-identifier hs-var">h</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-27"></span><span>    </span><span id="local-6989586621679057461"><span class="annot"><span class="annottext">Maybe TextEncoding
</span><a href="#local-6989586621679057461"><span class="hs-identifier hs-var">menc</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Handle -&gt; IO (Maybe TextEncoding)
</span><a href="../../base/src/GHC.IO.Handle.html#hGetEncoding"><span class="hs-identifier hs-var">hGetEncoding</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679057462"><span class="hs-identifier hs-var">h</span></a></span><span>
</span><span id="line-28"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">(TextEncoding -&gt; String) -&gt; Maybe TextEncoding -&gt; Maybe String
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="../../base/src/GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; String
</span><a href="../../base/src/GHC.IO.Encoding.Types.html#textEncodingName"><span class="hs-identifier hs-var hs-var">textEncodingName</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe TextEncoding
</span><a href="#local-6989586621679057461"><span class="hs-identifier hs-var">menc</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-29"></span><span>        </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span id="local-6989586621679057459"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679057459"><span class="hs-identifier hs-var">name</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Char
</span><span class="hs-char">'/'</span></span><span> </span><span class="annot"><span class="annottext">Char -&gt; String -&gt; Bool
forall (t :: * -&gt; *) a. (Foldable t, Eq a) =&gt; a -&gt; t a -&gt; Bool
</span><a href="../../base/src/Data.Foldable.html#notElem"><span class="hs-operator hs-var">`notElem`</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679057459"><span class="hs-identifier hs-var">name</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-30"></span><span>            </span><span id="local-6989586621679057457"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679057457"><span class="hs-identifier hs-var">enc'</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO TextEncoding
</span><a href="../../base/src/GHC.IO.Encoding.html#mkTextEncoding"><span class="hs-identifier hs-var">mkTextEncoding</span></a></span><span> </span><span class="annot"><span class="annottext">(String -&gt; IO TextEncoding) -&gt; String -&gt; IO TextEncoding
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679057459"><span class="hs-identifier hs-var">name</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;//TRANSLIT&quot;</span></span><span>
</span><span id="line-31"></span><span>            </span><span class="annot"><span class="annottext">Handle -&gt; TextEncoding -&gt; IO ()
</span><a href="../../base/src/GHC.IO.Handle.html#hSetEncoding"><span class="hs-identifier hs-var">hSetEncoding</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679057462"><span class="hs-identifier hs-var">h</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679057457"><span class="hs-identifier hs-var">enc'</span></a></span><span>
</span><span id="line-32"></span><span>        </span><span class="annot"><span class="annottext">Maybe String
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-33"></span></pre></body></html>